/*
 * $Id$
 */
package py.windmill.action;

import java.net.URLDecoder;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author myang
 */
public class HttpRequest implements Request {

    private HttpServletRequest _req;
    private HttpSession _session;

    private Response _resp;

    private HttpMethod _method;

    private Map<String, String> _params = new HashMap<String, String>();

    private String _uri;

    public HttpRequest(final HttpServletRequest req, final HttpServletResponse resp) {
        _req = req;
        _resp = new HttpResponse(resp, this);
        _session = req.getSession();
        final String path = req.getRequestURI();
        try {
            _uri = URLDecoder.decode(path, "utf-8");
        }
        catch (Exception ex) {
            _uri = path;
        }
        _method = HttpMethod.valueOf(req.getMethod());
    }

    @Override
    public HttpSession session() {
        return _session;
    }

    @Override
    public Request session(final String name, final Object val) {
        _session.setAttribute(name, val);
        return this;
    }

    @Override
    public Object session(final String name) {
        return _session.getAttribute(name);
    }

    /**
     * 
     * @param m
     * @return 
     */
    @Override
    public boolean is(final HttpMethod m) {
        return m == HttpMethod.valueOf(_req.getMethod());
    }

    /**
     * 
     * @return 
     */
    @Override
    public String requestUri() {
        return _uri;
    }

    /**
     * 
     * @param m
     * @return 
     */
    @Override
    public HttpRequest params(final Map<String, String> m) {
        _params.putAll(m);
        return this;
    }

    @Override
    public String param(final String name) {
        final String v = _params.get(name);
        return v != null ? v : _req.getParameter(name);
    }

    @Override
    public boolean accepts(final MimeType t) {
        final String s = accepts();
        return s.indexOf(t.type()) != -1 || s.indexOf(MimeType.ALL.type()) != -1;
    }

    @Override
    public String accepts() {
        return header("Accept");
    }

    @Override
    public String header(String name) {
        return _req.getHeader(name);
    }

    @Override
    public void forward(final String path) { //, final Response resp) {
        try {
            _req.getRequestDispatcher(path)
                    .forward(_req, _resp.containerResponse());
        }
        catch (Exception ex) {
            ;
        }
    }

    @Override
    public Request attr(final String name, final Object val) {
        _req.setAttribute(name, val);
        return this;
    }

    @Override
    public Object context(final String name) {
        return _req.getSession().getServletContext().getAttribute(name);
    }

    @Override
    public Object attr(final String name) {
        return _req.getAttribute(name);
    }

    @Override
    public Request context(final String name, final Object val) {
        _req.getSession().getServletContext().setAttribute(name, val);
        return this;
    }

    @Override
    public int getInt(final String name) {
        return Integer.parseInt(param(name));
    }

    @Override
    public Response response() {
        return _resp;
    }

    @Override
    public HttpMethod method() {
        return _method;
    }
}
